package compare;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Product implements Comparable {

    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    /**
     * 当前类需要实现Comparable中的抽象方法: CompareTo(Object o)
     * 在此方法中,指明如何判断当前类的对象的大小
     *
     * @param o the object to be compared.
     * @return 正数, 当前对象大;负数,当前对象小
     */
    @Override
    public int compareTo(Object o) {
        if (o == this)
            return 0;
        if (o instanceof Product p) {
            int compare = Double.compare(this.price, p.price);
            return compare == 0 ? this.name.compareToIgnoreCase(p.name) : compare;
        }
        // 手动抛出一个异常类的对象
        throw new RuntimeException("类型不匹配.");
    }


}
